﻿/*
 * Copyright 2014 Francois Karman
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Quartz.WebManager.Model
{
    /// <summary>
    /// Represents an instance of a job.
    /// </summary>
    public class JobDetail
    {
        /// <summary>
        /// The triggers associated with the job.
        /// </summary>
        private readonly ICollection<Trigger> triggers;

        /// <summary>
        /// The properties that will composes the job map.
        /// </summary>
        private readonly IDictionary<string, string> properties;

        /// <summary>
        /// The definition of the job.
        /// </summary>
        private JobDefinition definition;

        /// <summary>
        /// Initializes a new instance of the <see cref="JobDetail"/> class.
        /// </summary>
        public JobDetail()
        {
            this.triggers = new LinkedList<Trigger>();
            this.properties = new Dictionary<string, string>();
        }

        /// <summary>
        /// Gets the properties that will composes the job map.
        /// </summary>
        public IDictionary<string, string> Properties
        {
            get { return this.properties; }
        }

        /// <summary>
        /// Gets the triggers associated with the job.
        /// </summary>
        /// <remarks>
        /// This property is not used when saving a <c>JobDetail</c> instance.
        /// </remarks>
        public ICollection<Trigger> Triggers
        {
            get { return this.triggers; }
        }

        /// <summary>
        /// Gets or sets the definition of the job.
        /// </summary>
        /// <remarks>
        /// This property is used for display - not for add or update.
        /// </remarks>
        public JobDefinition Definition
        {
            get { return this.definition; }
            set
            {
                this.definition = value;
                foreach (var property in this.definition.Properties)
                {
                    if (!this.properties.ContainsKey(property.Name))
                    {
                        this.properties.Add(property.Name, null);
                    }
                }
            }
        }

        /// <summary>
        /// Gets or sets the type of the job instance.
        /// </summary>
        [Required]
        public string JobType { get; set; }

        /// <summary>
        /// Gets or sets the name of the job instance.
        /// </summary>
        [Required]
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the group of the job instance.
        /// </summary>
        [Required]
        public string Group { get; set; }

        /// <summary>
        /// Gets or sets the description of the job instance, if any.
        /// </summary>
        public string Description { get; set; }
    }
}
